package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.ExportProductDao;
import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Service(timeout = 1000000)
public class PackingServiceImpl implements PackingService {

    @Autowired
    private ExportDao exportDao;
    @Autowired
    private ExportProductDao exportProductDao;
    @Autowired
    private PackingDao packingDao;

    /**
     *  生成装箱单
     * @param exportIds 报运单id数组
     * @return
     */
    @Override
    public Packing createPacking(String[] exportIds) {
        //定义装箱单对象
        Packing packing = new Packing();
        packing.setPackingListId(UUID.randomUUID().toString());
        String ids = "";
        for (int i = 0; i < exportIds.length; i++) {
            ids += (i==exportIds.length-1) ? (exportIds[i]) : (exportIds[i]+",");
        }
        packing.setExportIds(ids);
        //设置自动计算属性初始值
        Double totalVolume = 0d;
        Double netWeights = 0d;
        Double grossWeights = 0d;
        Double packingMoney = 0d;
        //打断储存报运合同号，用" "隔开
        String exportNos = "";
        //遍历exportIds,得到每一个报运单对象
        for (String exportId : exportIds) {
            Export export = exportDao.selectByPrimaryKey(exportId);
            //设置合同号
            exportNos += export.getCustomerContract() + " ";
            //根据exportId，查询报运商品
            ExportProductExample example = new ExportProductExample();
            example.createCriteria().andExportIdEqualTo(exportId);
            List<ExportProduct> products = exportProductDao.selectByExample(example);
            //遍历报运商品集合，维护装箱单中的自动计算属性值
            for (ExportProduct product : products) {
                if (product.getBoxNum() != null && product.getSizeLength() != null
                        && product.getSizeWidth() != null && product.getSizeHeight() != null) {
                    totalVolume += product.getBoxNum() * product.getSizeLength()
                            * product.getSizeWidth() * product.getSizeHeight();
                }
                if (product.getNetWeight() != null) {
                    netWeights += product.getNetWeight();
                }
                if (product.getGrossWeight() != null) {
                    grossWeights += product.getGrossWeight();
                }
            }
        }
        packingMoney = grossWeights * 520;
        //设置合同号
        packing.setExportNos(exportNos);
        //将自动计算的属性设置到装箱单
        packing.setTotalVolume(totalVolume);
        packing.setNetWeights(netWeights);
        packing.setGrossWeights(grossWeights);
        packing.setPackingMoney(packingMoney);
        //返回装箱单对象
        return packing;
    }

    /**
     * 添加装箱单
     * @param packing 装箱单
     */
    @Override
    public void addPacking(Packing packing) {
        //设置packing的state为0
        packing.setState(0);
        //修改export的state为4，"已装箱"    (后期数据字典优化)
        String[] exportIds = packing.getExportIds().split(",");
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andIdIn(Arrays.asList(exportIds));
        List<Export> exportList = exportDao.selectByExample(exportExample);
        for (Export export : exportList) {
            export.setState(4);
            exportDao.updateByPrimaryKeySelective(export);
        }
        //将装箱单插入到装箱表
        packingDao.insertSelective(packing);
    }

    /**
     * 分页查询
     * @param packingExample 分页查询条件对象
     * @param pageNum 当前页数
     * @param pageSize 分页大小
     * @return
     */
    @Override
    public PageInfo<Packing> findByPage(PackingExample packingExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Packing> list = packingDao.selectByExample(packingExample);
        return new PageInfo<>(list);
    }

    /**
     *  根据主键查询
     * @param packingListId 装箱单id
     * @return
     */
    @Override
    public Packing findById(String packingListId) {
        return packingDao.selectByPrimaryKey(packingListId);
    }

    /**
     *  根据主键删除
     * @param packingListId 装箱单id
     */
    @Override
    public void delete(String packingListId) {
        packingDao.deleteByPrimaryKey(packingListId);
    }
}
